(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数
	//获取列表
	function getlist(curpage,pagenum){
		var leveling_name=$("#leveling_name").val();
		var leveling_account=$("#leveling_account").val();
		var user_id=$("#user_id").val();
		var le_id=$("#le_id").val();
		var leveling_type=$("#leveling_type").val()==0?"":$("#leveling_type").val();
		var user_name=$("#user_name").val();
		var order_id=$("#order_id").val();
		var startTime=$("#datepicker").val();
		var endTime=$("#datepicker2").val();
	   $.ajax({
			// url:'http://web.icecn.net/leveling/order/getOrderVoList.json',
			url:httpOrigin + '/leveling/order/getOrderVoList.json',
			
			type:'POST',
			async:true,
			data:{
                offset:pagenum*(curpage-1),
				limit:pagenum,
				leveling_name:leveling_name,
				leveling_account:leveling_account,
				user_id:user_id,
				le_id:le_id,
				leveling_type:leveling_type,
				user_name:user_name,
				order_id:order_id,
				startTime:startTime,
				endTime:endTime
			},
			xhrFields:{withCredentials:true},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
				    dataset=data.t.result;
					if(dataset && dataset.length > 0){
					   for(var i=0;i<dataset.length;i++){
							switch(dataset[i].order_status){
							 case 1 :{
								dataset[i].order_status="待支付" ;
								break;
							 }
							 case 2 :{
								dataset[i].order_status="准备中" ;
								break;
							 }
							 case 3 :{
								dataset[i].order_status="待确认" ;
								break;
							 }
							 case 4 :{
								dataset[i].order_status="已完成" ;
								break;
							 }
							 case 5 :{
								dataset[i].order_status="等待大神退款" ;
								break;
							 }
							 case 6 :{
								dataset[i].order_status="等待客服仲裁" ;
								break;
							 }
							 case 7 :{
								dataset[i].order_status="已退款" ;
								break;
							 }
							 case 8 :{
								dataset[i].order_status="已取消" ;
								break;
							 }
							 case 9 :{
								dataset[i].order_status="已评价" ;
								break;
							 }
							 case 10 :{
								dataset[i].order_status="退款失败" ;
								break;
							 }
						  }	   
					   }
					   var html = template('datalist',{dataset:dataset});
					   $("#example1").html(html);
					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage==totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}else{
						$("#example1").html("<tbody><tr><td>查不到数据</td></tr></tbody>")
					};
				}
			}
	   });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
				    	curpage=parseInt($(".curpage").val());
					    getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//搜索按钮
			$(".btnsearch").click(function(){
			  curpage=1;
			  getlist(curpage,pagenum)
			})
			//取消订单按钮
			$(".content-wrapper").on("click", '.cancel', function() {
				$("#modal-cancel .modal-body").attr("data-id",$(this).attr("data-id"))
			})
			//确认取消订单
			$(".btncancel").click(function(){
			   var id=$("#modal-cancel .modal-body").attr("data-id");
			   $.ajax({
					url:'http://web.icecn.net/leveling/order/cancelOrder.json',
					type:'POST',
					async:true,
					data:{
						id:id
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-cancel").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert("取消失败")	
						}
					}
			   });
		    })
			//查看退款理由
			$(".content-wrapper").on("click", '.view', function() {
				$("#complain_type").val("")
				$("#complain_info").val("")
				$(".imgdiv").html("")
				var id=$(this).attr("data-id")
				$.ajax({
					url:'http://web.icecn.net/complain/getComplainForOrder.json',
					type:'POST',
					async:true,
					data:{
						or_id:id
					},
			         xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#complain_type").val(data.t.complain_type)
							$("#complain_info").val(data.t.complain_info)
							var complainPic=data.t.complainPic;
							var img="";
							if(complainPic.length>0){
							  for(var i=0;i<complainPic.length;i++){
								  img+="<img src='"+complainPic[i].picUrl+"' width='150px'/>"
							  }
							}
							$(".imgdiv").html(img)
						}
					}
			   });
			})
			//同意退款
			$(".content-wrapper").on("click", '.agree', function() {
				$("#modal-agree .modal-body").attr("data-id",$(this).attr("data-id"))
				$("#modal-agree .modal-body h4").html("订单金额："+$(this).attr("data-price"))
			})
			//确认同意退款
			$(".btnagree").click(function(){
			   var id=$("#modal-agree .modal-body").attr("data-id");
			   var return_price=$("#return_price").val()
			   $.ajax({
					url:'http://web.icecn.net/complain/operateComplain.json',
					type:'POST',
					async:true,
					data:{
						or_id:id,
						return_price:return_price
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-agree").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
			//拒绝退款
			$(".content-wrapper").on("click", '.Refuse', function() {
				$("#modal-Refuse .modal-body").attr("data-id",$(this).attr("data-id"))
			})
			//确认拒绝退款
			$(".btnRefuse").click(function(){
			   var id=$("#modal-Refuse .modal-body").attr("data-id");
			   var msg=$("#msg").val()
			   $.ajax({
					url:'http://web.icecn.net/complain/refuseComplain.json',
					type:'POST',
					async:true,
					data:{
						or_id:id,
						msg:msg
					},
					xhrFields:{withCredentials:true},
					dataType: "json",
					success: function(data){
						if(data.code === 200 ){
							$("#modal-Refuse").modal('hide');
							getlist(curpage,pagenum);
						}else{
						    alert(data.msg)	
						}
					}
			   });
		    })
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum)
		}
	};
	Page.init();
})();